package nonre.sgy.hessiananno;

import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

public final class HessianBeanNameGenerator extends AnnotationBeanNameGenerator {
    
	/**
	 * 注解中定义的value作为hessian服务的名称
	 */
	@Override
    protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
        
        AnnotationMetadata amd = annotatedDef.getMetadata();
        Set<String> types = amd.getAnnotationTypes();
        String beanName = null;
        for(String type:types){
//            System.out.println(type);
            if(type.equals(HessianService.class.getName())){
                Map<String, Object> attributes = amd.getAnnotationAttributes(type);
                String value = (String) attributes.get("value");
                if (StringUtils.hasLength(value)) {
                    beanName = value;
//                    System.out.println("beanName:"+beanName);
                }
            }
        }
        
        return beanName;
    }
    
}
